package com.mcptt.db.model;

import java.io.Serializable;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.comm.Common;
import com.cloudptt.api.models.lbs.LocationData;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.db.DbConst;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

public class MapPointInfo implements Serializable 
{
	public static final int MapPointType_GPS = 0;

	private UserInfo sender = null;  //发送方属性
	private String senderId = null;
	private LocationData point = null;
	private long last_time = 0;
	private String location_info = null;
	private int location_type = -1;
	private String ip = null;


	public MapPointInfo(String senderId, int type, long time, LocationData point, String info,String ip) {

		//this.receiver = receiver;
		this.senderId = senderId;
		//this.isSendRequest = true;
		this.point = point;
		this.last_time = time;
		this.location_type = type;
		this.location_info = info;
		this.ip = ip;
	}

	public MapPointInfo(Cursor c) 
	{
		//this.receiver = new UserInfo();
		//this.receiver.setUserType(UserInfo.UserType_User);

		this.sender = new UserInfo();
		this.sender.setUserType(UserInfo.UserType_User);

		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args, c);
	}
	public final void createFromContentValue(ContentValues args, Cursor c) 
	{
		String mcId = args.getAsString(DbConst.COL_Id);
		String name = args.getAsString(DbConst.COL_Name);
		if(mcId != null
				&& name != null) {
			sender = new UserInfo(mcId,name);
		}
		this.senderId = args.getAsString(DbConst.COL_Id);
//		String mcvideoId = args.getAsString(DbConst.COL_McvideoId);
//		String mcdataId = args.getAsString(DbConst.COL_McdataId);
		String sAttr = args.getAsString(DbConst.COL_Attr);
		
//		if(sender != null
//				&& senderMcpttId != null) 
//		{
//			sender.setMcpttId(senderMcpttId);
//		}
//		if(sender != null
//				&& mcvideoId != null) 
//		{
//			sender.setMcvideoId(mcvideoId);
//		}
//		if(sender != null
//				&& mcdataId != null) 
//		{
//			sender.setMcdataId(mcdataId);
//		}
		if(sender != null
				&& sAttr != null) 
		{
			int value = CheckData.parseInt(sAttr);
			sender.setAttr(value);
		}
		//this.isSendRequest = args.getAsBoolean(DbConst.COL_IsSendRequest);
		//double lattitude = args.getAsDouble(DbConst.COL_Lattitude);
		//double longitude = args.getAsDouble(DbConst.COL_Longitude);
		//double height = args.getAsDouble(DbConst.COL_Altitude);
		
		double lattitude = c.getDouble(c.getColumnIndexOrThrow(DbConst.COL_Lattitude));
		double longitude = c.getDouble(c.getColumnIndexOrThrow(DbConst.COL_Longitude));
		double height = c.getDouble(c.getColumnIndexOrThrow(DbConst.COL_Altitude));
		
		last_time = args.getAsLong(DbConst.COL_Time);
		location_type = args.getAsInteger(DbConst.COL_Loc_Type);
		location_info = args.getAsString(DbConst.COL_Loc_Info);

		this.point = new LocationData(lattitude, longitude, height);
	}

	/**
	 * Pack the object content value to store
	 * 
	 * @return The content value representing the message
	 */
	 public ContentValues getContentValues() {

		ContentValues cv = new ContentValues();
		//		cv.put(DbConst.COL_ReceiverUId, receiver.getuId());
		//		cv.put(DbConst.COL_ReceiverNum, receiver.getMcId());
		//		cv.put(DbConst.COL_SenderUId, sender.getuId());
		cv.put(DbConst.COL_SenderId, senderId);
		//		cv.put(DbConst.COL_IsSendRequest, isSendRequest);
		if(point != null)
		{
			cv.put(DbConst.COL_Lattitude, point.getLattitude());
			cv.put(DbConst.COL_Longitude, point.getLongitude());
			cv.put(DbConst.COL_Altitude, point.getAltitude());
		}
		
		if(last_time != 0){
			cv.put(DbConst.COL_Time, last_time);
		}
		
		if(location_type != -1){
			cv.put(DbConst.COL_Loc_Type, location_type);
		}
		
		if(location_info != null){
			cv.put(DbConst.COL_Loc_Info, location_info);
		}
		
		return cv;
	 }
	 
	 public ContentValues getTrackContentValues() {

			ContentValues cv = new ContentValues();
			//		cv.put(DbConst.COL_ReceiverUId, receiver.getuId());
			//		cv.put(DbConst.COL_ReceiverNum, receiver.getMcId());
			//		cv.put(DbConst.COL_SenderUId, sender.getuId());
			cv.put(DbConst.COL_SenderId, senderId);
			//		cv.put(DbConst.COL_IsSendRequest, isSendRequest);
			if(point != null)
			{
				cv.put(DbConst.COL_Lattitude, point.getLattitude());
				cv.put(DbConst.COL_Longitude, point.getLongitude());
				cv.put(DbConst.COL_Altitude, point.getAltitude());
				
				if(point.getLattitude() == 0 && point.getLongitude() == 0){
					cv.put(DbConst.COL_Valid, 0);
				}else{
					cv.put(DbConst.COL_Valid, 1);
				}
			}
			
			if(last_time != 0){
				cv.put(DbConst.COL_Time, last_time);
			}
			
//			if(location_type != -1){
//				cv.put(DbConst.COL_Loc_Type, location_type);
//			}
			
//			if(location_info != null){
//				cv.put(DbConst.COL_Loc_Info, location_info);
//			}
			
			return cv;
		 }

	 public ContentValues getLoginContentValues(){
			ContentValues cv = new ContentValues();
			
			cv.put(DbConst.COL_SenderId, senderId);
			cv.put(DbConst.COL_State, 1);
			cv.put(DbConst.COL_Ip, ip);
			return cv;
		}
	public String getSenderId() {
		return senderId;
	}

	public UserInfo getSender() {
		return sender;
	}
	
	public boolean isValid()
	{
		return point.isValid();
	}
	public boolean isSameOne(AddrInfo mcIdInfo)
	{
		return sender.isSameOne(mcIdInfo);
	}

	public String getDscp()
	{
		return sender.getDscp();
	}

	public String toString()
	{
		StringBuffer buffer = new StringBuffer("MapPointInfo:");
		//buffer.append("receiver = ").append(receiver).append(",");
		buffer.append("sender = ").append(sender).append(",");
		buffer.append("senderId = ").append(senderId).append(",");
		buffer.append("point = ").append(point).append(",");
		buffer.append("last = ").append(last_time).append(",");
		buffer.append("type = ").append(location_type).append(",");
		buffer.append("info = ").append(location_info).append(";");
		buffer.append("ip = ").append(ip).append(";");

		return buffer.toString();
	}
	
	public LocationData getLocationData(){
		return point;
	}
	
	public String getTime(){
		String formatTime = Common.getFormatDate(String.valueOf(last_time));
		return formatTime;
	}
}
